package com.hoki.zj.hrm.service.impl;

import com.hoki.zj.hrm.domain.LoginUser;
import com.hoki.zj.hrm.mapper.LoginUserMapper;
import com.hoki.zj.hrm.properties.OAuth2ClientDetails;
import com.hoki.zj.hrm.properties.OAuth2ClientDetailsProperties;
import com.hoki.zj.hrm.service.ILoginUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hoki.zj.hrm.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shukei
 * @since 2021-09-12
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {

    @Autowired
    private OAuth2ClientDetailsProperties detailsProperties;

    /**
     * 1.前台/后台登录(发送请求获取token)
     * @param loginUser loginUser
     */
    @Override
    public Map<String, String> loginToGetToken(LoginUser loginUser) {
        String baseUrl = "http://localhost:4020/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&password=%s";
        // 1.拼接url,密码模式获取token,client_id,  client_secret, grant_type, username=yhptest1 password=123456
        OAuth2ClientDetails clientDetails =
                detailsProperties.getOAuth2ClientDetails(loginUser.getType()); // 根据type获取OAuth2ClientDetails
        String clientId = clientDetails.getClientId(); // clientDetails获取client_id,
        String clientSecret = clientDetails.getClientSecret(); // 获取client_secret
        // 拼接
        String getTokenUrl = String.format(baseUrl, clientId, clientSecret, loginUser.getUsername(), loginUser.getPassword());
        // 2.发送请求获取token
        return HttpUtil.sendPost(getTokenUrl);

    }
}
